/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.actor;

import org.openaecfm.model.actor.ActorRole;
import org.openaecfm.model.actor.Address;
import org.openaecfm.model.actor.Organization;
import org.openaecfm.service.actor.IOrganizationManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class OrganizationIO extends
		AbstractEntityInstanceInputImpl<Organization> implements
		IEntityInstanceOutput<Organization> {

	private IOrganizationManager manager;

	public void setManager(IOrganizationManager manager) {
		this.manager = manager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Organization pojo, IAppDataManager appDataManager) {

		SimpleEntityInstance ei = new SimpleEntityInstance("IFCORGANIZATION",
				appDataManager.getName(pojo));

		// Set Identification
		if (pojo.getIdentification() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getIdentification(),
					StringParameter.UTF_8));
		}

		// Set Name
		ei.addParameter(new StringParameter(pojo.getName(),
				StringParameter.UTF_8));

		// Set Description
		if (pojo.getDescription() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getDescription(),
					StringParameter.UTF_8));
		}

		// Set Roles
		if (pojo.getRoles() == null || pojo.getRoles().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (ActorRole ar : pojo.getRoles()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(ar)));
			}
			ei.addParameter(l);
		}

		// Set Addresses
		if ((pojo.getAddresses() == null) || (pojo.getAddresses().isEmpty())) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Address ad : pojo.getAddresses()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(ad)));
			}
			ei.addParameter(l);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Organization doCreateObject() {
		return new Organization();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Organization pojo,
			IAppDataManager appDataManager) {

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Roles
		Parameter ro_v = sei.getParameter(3);
		if (ro_v.getNodeType() == NodeType.LIST) {
			ListParameter l_p = (ListParameter) ro_v;
			for (Parameter p : l_p) {
				EntityInstanceNameParameter n_p = (EntityInstanceNameParameter) p;
				ActorRole ar = (ActorRole) appDataManager.getObject(n_p
						.toInstanceName());
				pojo.getRoles().add(ar);
			}
		}

		// Set Addresses
		Parameter ad_v = sei.getParameter(4);
		if (ad_v.getNodeType() == NodeType.LIST) {
			ListParameter l_p = (ListParameter) ad_v;
			for (Parameter p : l_p) {
				EntityInstanceNameParameter n_p = (EntityInstanceNameParameter) p;
				Address add = (Address) appDataManager.getObject(n_p
						.toInstanceName());
				manager.addAddress(pojo, add);
			}
		}
	}

	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			Organization pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Identification
		Parameter id_pro = sei.getParameter(0);
		if (id_pro.getNodeType() == NodeType.STRING) {
			StringParameter s_p = (StringParameter) id_pro;
			pojo.setIdentification(s_p.toUTF8());
		}

		// Set Name
		StringParameter name_pro = (StringParameter) sei.getParameter(1);
		pojo.setName(name_pro.toUTF8());

		// Set Description
		Parameter d_pro = sei.getParameter(2);
		if (d_pro.getNodeType() == NodeType.STRING) {
			StringParameter s_p = (StringParameter) d_pro;
			pojo.setDescription(s_p.toUTF8());
		}
	}
}
